package zhtt.cn.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;

public class ResourceLoaderExample {

    public static void main() {
        try {
            // 获取类加载器
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

            // 从类加载器中获取特定目录下的所有资源 URL
            Enumeration<URL> resources = classLoader.getResources("resources");

            // 遍历资源并读取文件内容
            while (resources.hasMoreElements()) {
                URL resourceUrl = resources.nextElement();
                InputStream inputStream = resourceUrl.openStream();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
                    System.out.println("Reading file: " + resourceUrl);
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                    System.out.println("-------------------");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
